//修改密码
//  ChangeViewController.m
//  BigToBig
//
//  Created by guochuangkeshi on 16/4/27.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "ChangeViewController.h"
#import "HomeViewController.h"
@interface ChangeViewController ()<UITextFieldDelegate>


{
    UITextField *oldTextF;//旧密码
    UITextField *newTextF;//新密码
    UITextField *sureTextF;//确认新密码
    UIScrollView *UiscrollView;
}


@end

@implementation ChangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLab.text = @"修改密码";
    titleLab.textColor = [UIColor whiteColor];
    [self setBackBar];
    titleLab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLab;
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(77, 155, 255, 1);
    
    self.view.backgroundColor = GRAYCOLOR;
    
    [self setBackBar];
    
    [self addTextF];
    
    [self addSureBtn];
}

#define mark 返回
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
}

- (void)addTextF{
    
    
    UiscrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    UiscrollView.contentSize = CGSizeMake(0, kScreenHeight+ 80);
    [self.view addSubview:UiscrollView];
    
    UIView *backgroungView1=[[UIView alloc]initWithFrame:CGRectMake(0, 15, kScreenWidth, 50)];
    backgroungView1.backgroundColor = [UIColor whiteColor];
    [UiscrollView addSubview:backgroungView1];
    
    oldTextF =[[UITextField alloc]initWithFrame:CGRectMake(20, 0, kScreenWidth-20, 50)];
    oldTextF.clearButtonMode = UITextFieldViewModeWhileEditing;
    oldTextF.tintColor=[UIColor lightGrayColor];
    oldTextF.borderStyle = UITextBorderStyleNone;
    oldTextF.placeholder = @"请输入旧密码";
    [oldTextF setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    oldTextF.delegate = self;
    [backgroungView1 addSubview:oldTextF];
    
    UIView *backgroungView2=[[UIView alloc]initWithFrame:CGRectMake(0, backgroungView1.maxY+15, kScreenWidth, 50)];
    backgroungView2.backgroundColor = [UIColor whiteColor];
    [UiscrollView addSubview:backgroungView2];
    
    newTextF =[[UITextField alloc]initWithFrame:CGRectMake(20, 0, kScreenWidth-20, 50)];
    newTextF.tintColor=[UIColor lightGrayColor];
    newTextF.clearButtonMode = UITextFieldViewModeWhileEditing;
    newTextF.borderStyle = UITextBorderStyleNone;
    newTextF.placeholder = @"请输入新密码";
    [newTextF setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    newTextF.delegate = self;
    [backgroungView2 addSubview:newTextF];
    
    UIView *backgroungView3=[[UIView alloc]initWithFrame:CGRectMake(0, backgroungView2.maxY+15, kScreenWidth, 50)];
    backgroungView3.backgroundColor = [UIColor whiteColor];
    [UiscrollView addSubview:backgroungView3];
    
    sureTextF =[[UITextField alloc]initWithFrame:CGRectMake(20, 0, kScreenWidth-20, 50)];
    sureTextF.tintColor=[UIColor lightGrayColor];
    sureTextF.clearButtonMode = UITextFieldViewModeWhileEditing;
    sureTextF.borderStyle = UITextBorderStyleNone;
    sureTextF.placeholder = @"请再次输入新密码";
    [sureTextF setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    sureTextF.delegate = self;
    [backgroungView3 addSubview:sureTextF];
}

- (void)addSureBtn{
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [sureBtn setFrame:CGRectMake(10,210, kScreenWidth-20, 50)];
    [sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitle:@"确定修改" forState:UIControlStateNormal];
    sureBtn.backgroundColor = BASICCOLOR;
    [sureBtn.layer setMasksToBounds:YES];
    [sureBtn.layer setCornerRadius:5];
    
    [UiscrollView addSubview:sureBtn];
    
    
}

- (void)sureBtnClick:(UIButton *)btn{
    
    NSUserDefaults* inputSecreate = [NSUserDefaults standardUserDefaults];
    NSString *OldSecreate = [inputSecreate objectForKey:@"Secreate"];
    NSUserDefaults* userID = [NSUserDefaults standardUserDefaults];
    NSString *Uid = [userID objectForKey:@"userID"];
    if (oldTextF.text.length == 0) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请输入旧密码";
        [hud hide:YES afterDelay:1];
        
    }else if (oldTextF.text.length<6||oldTextF.text.length>18){
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"密码长度需大于5位小于17位";
        [hud hide:YES afterDelay:1];
        
    }else if (newTextF.text.length == 0){
    
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请输入新密码";
        [hud hide:YES afterDelay:1];
   
    }else if (newTextF.text.length <6||newTextF.text.length >18){
    
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"密码长度需大于5位小于17位";
        [hud hide:YES afterDelay:1];
        
    }else if(sureTextF.text.length == 0){
    
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请再次输入密码";
        [hud hide:YES afterDelay:1];
        
    }else if (sureTextF.text.length <6||sureTextF.text.length>18)
    {
    
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"密码长度需大于5位小于17位";
        [hud hide:YES afterDelay:1];
        
    }else if(![newTextF.text isEqualToString:sureTextF.text]) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"两次密码输入不一致";
        [hud hide:YES afterDelay:1];

    
    }else if(![OldSecreate isEqualToString:oldTextF.text]){
    
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"旧密码不正确";
        [hud hide:YES afterDelay:1];
        
    }else{

        
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        NetworkStatus status = [reach currentReachabilityStatus];
        
        if (status == NotReachable) {
            
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
            HUD.labelText = @"请联网后修改密码";
            HUD.mode = MBProgressHUDModeText;
            [HUD hide:YES afterDelay:1.0];
            
            return;
            
        }

        NSDictionary *params = @{@"id":Uid,@"old_password":oldTextF.text,@"new_password":newTextF.text};
        
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/user/edit_password?" params:params success:^(id json) {
            
            [UIView animateWithDuration:2.0 animations:^{
                
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"修改密码成功请重新登录";
                [hud hide:YES afterDelay:1];
                
            } completion:^(BOOL finished) {
                
                [NSThread sleepForTimeInterval:1.0];
                UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[HomeViewController new]];
                KeyWindow.rootViewController = nav;
                
                
            }];
            
        } failure:^(NSError *error) {
            
            
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"修改密码失败";
            [hud hide:YES afterDelay:1];
        }];

       
    }

}
#pragma mark 返回事件
- (void)backController:(UIButton *)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

   return  [textField endEditing:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [oldTextF endEditing:YES];
    [newTextF endEditing:YES];
    
}


@end
